﻿// <copyright file="NullableIntTypeConverter.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-01-26</date>
// <summary>NullableIntValueConverter class definition</summary>

namespace Microsoft.WebAnalytics
{
    using System;
    using System.ComponentModel;

    /// <summary>
    /// Use this on any XAML-serializable public property of type int?
    /// </summary>
    /// <example>
    /// <code>
    /// class MyClass
    /// {
    ///    [TypeConverter(typeof(NullableIntTypeConverter))]
    ///    public int? Value { get; set; }
    /// }
    /// </code>
    /// </example>
    public class NullableIntTypeConverter : TypeConverter
    {
        #region Methods
        /// <summary>
        /// returns true if the sourceType is a <see cref="System.String"/>
        /// </summary>
        /// <param name="context">the conversion context</param>
        /// <param name="sourceType">the source type</param>
        /// <returns>true if the sourceType is <see cref="System.String"/></returns>
        public override bool CanConvertFrom(ITypeDescriptorContext context, Type sourceType)
        {
            if (sourceType == typeof(string))
            {
                return true;
            }

            return base.CanConvertFrom(context, sourceType);
        }

        /// <summary>
        /// Returns true if the destination type is a <see cref="System.String"/>
        /// </summary>
        /// <param name="context">the conversion context</param>
        /// <param name="destinationType">the destination type</param>
        /// <returns>true if the destination type is a <see cref="System.String"/></returns>
        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                return true;
            }

            return base.CanConvertTo(context, destinationType);
        }

        /// <summary>
        /// Converts from a string to a nullable int
        /// </summary>
        /// <param name="context">the conversion context</param>
        /// <param name="culture">the culture</param>
        /// <param name="value">the value to convert</param>
        /// <returns>a nullable int (int?)</returns>
        public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
        {
            var stringValue = value as string;

            if (string.IsNullOrEmpty(stringValue))
            {
                return new int?();
            }
            else
            {
                return new int?(int.Parse(stringValue, culture));
            }
        }

        /// <summary>
        /// Converts from a nullable int (int?) to a string
        /// </summary>
        /// <param name="context">the conversion context</param>
        /// <param name="culture">the culture</param>
        /// <param name="value">the value to convert</param>
        /// <param name="destinationType">the destination type</param>
        /// <returns>a string or null</returns>
        public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
        {
            if (destinationType == typeof(string))
            {
                int? nullableIntValue = value as int?;

                if (nullableIntValue.HasValue)
                {
                    return nullableIntValue.Value.ToString(culture);
                }
                else
                {
                    return null;
                }
            }

            return base.ConvertTo(context, culture, value, destinationType);
        }
        #endregion
    }
}
